<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-检测文件改动 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-检测文件改动</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>有两台Linux服务器A和B，假如A可以直接ssh到B，不用输入密码。A和B都有一个目录叫做/data/web/ 这下面有很多文件，当然我们不知道具体有几层子目录，假若之前A和B上该目录下的文件都是一模一样的。但现在不确定是否一致了。固需要我们写一个脚本实现这样的功能，检测A机器和B机器/data/web/目录下文件的异同，我们以A机器上的文件作为标准。比如，假若B机器少了一个a.txt文件，那我们应该能够检测出来，或者B机器上的b.txt文件有过改动，我们也应该能够检测出来（B机器上多了文件我们不用考虑）。</p>
<p>提示： 使用核心命令  md5sum a.txt  算出md5值，去和B机器上的比较。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#假设A机器到B机器已经做了无密码登录设置</span></div><div class="line">dir=/data/web</div><div class="line"><span class="comment">##假设B机器的IP为192.168.0.100</span></div><div class="line">B_ip=192.168.0.100</div><div class="line">find <span class="variable">$dir</span> -<span class="built_in">type</span> f |xargs md5sum &gt;/tmp/md5.txt</div><div class="line">ssh <span class="variable">$B_ip</span> <span class="string">"find <span class="variable">$dir</span> -type f |xargs md5sum &gt;/tmp/md5_b.txt"</span></div><div class="line">scp <span class="variable">$B_ip</span>:/tmp/md5_b.txt /tmp</div><div class="line"></div><div class="line"><span class="keyword">for</span> f <span class="keyword">in</span> `awk <span class="string">'&#123;print $2&#125;'</span> /tmp/md5.txt`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> grep -q <span class="string">"<span class="variable">$f</span>"</span> /tmp/md5_b.txt</div><div class="line">    <span class="keyword">then</span></div><div class="line">        md5_a=`grep <span class="variable">$f</span> /tmp/md5.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">        md5_b=`grep <span class="variable">$f</span> /tmp/md5_b.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$md5_a</span> != <span class="variable">$md5_b</span> ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">             <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> changed."</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> deleted. "</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/29. shell 练习-检测文件改动/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
